<template>
	<view style="height: 100%;">
		<cu-custom :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{navTitle}}</block>
		</cu-custom>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption">
			<view v-for="(item,index) in dataList" :key="index">
				<list-user-item @click="jumpToUserProfile(item.user_id)" :userInfo="item"></list-user-item>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import ListUserItem from '@/components/list-user-item.vue'
	import {
		getTimeDistance,
		cutDateFromDateTime
	} from '@/api/utils.js'
	import {
		getUserAttentionList,
		getMyFansList,
		getOtherUserFansList,
		getMyNewFansList,
		markNewFansListRead
	} from '@/api/api.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import config from '@/api/config.js'
	export default {
		components: {
			ListUserItem,
		},
		name:"FansList",
		mixins: [MescrollMixin],
		data() {
			return {
				themeColor:this.ThemeColor,
				mode:1, //1我的关注，2他的粉丝，3我的粉丝
				otherUserId:null,
				dataList: [],
				total:0,
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					textNoMore: config.customNoMore,
					noMoreSize: 1, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
			}
		},
		onLoad(options) {
			if(options.mode !== null && options.mode !== undefined) {
				this.mode = options.mode
			}
			if(options.otherUserId !== null && options.otherUserId !== undefined) {
				this.otherUserId = options.otherUserId
			}
		},
		computed: {
			navTitle() {
				if(this.mode == 1) {
					return '我的关注('+this.total+')'
				}
				if(this.mode == 2) {
					return '新增粉丝('+this.total+')'
				}
				if(this.mode == 3) {
					return '我的粉丝('+this.total+')'
				}
			}
		},
		methods: {
			jumpToUserProfile(userId) 
			{
				uni.navigateTo({
					url: '/pages/profile/profile?userId=' + userId
				})
			},
			getDataList(pageIndex, pageSize) {
				if(this.mode == 1) {
					getUserAttentionList(pageIndex, pageSize).then(res => {
						this.total = res.data.total
						res.data.list.forEach(item => {
							item.show_time = cutDateFromDateTime(item.created_at)
						})
						let pageList = res.data.list
						if (pageIndex == 0) {
							this.dataList = pageList
						} else {
							this.dataList = this.dataList.concat(pageList)
						}
						let hasNext = pageList.length >= pageSize
						if (this.mescroll) {
							this.mescroll.endSuccess(this.dataList.length, hasNext);
						}
					}).catch(error => {
						if (this.mescroll) {
							this.mescroll.endErr()
						}
					})
				}else if(this.mode == 2) {
					getMyNewFansList(pageIndex, pageSize).then(res => {
						this.total = res.data.total
						let filterList = []
						res.data.list.forEach(item => {
							if(item !== null && item !== undefined) {
								item.show_time = cutDateFromDateTime(item.created_at)
								filterList.push(item)
							}
						})
						let pageList = filterList
						if (pageIndex == 0) {
							this.dataList = pageList
						} else {
							this.dataList = this.dataList.concat(pageList)
						}
						if(pageList.length > 0) {
							markNewFansListRead(res.data.id_list).then(res=>{
								console.log('新增粉丝消息已读')
							}).catch(error=>{
								
							})
						}
						let hasNext = res.data.list.length >= pageSize
						if (this.mescroll) {
							this.mescroll.endSuccess(this.dataList.length, hasNext);
						}
					}).catch(error => {
						if (this.mescroll) {
							this.mescroll.endErr()
						}
					})
				}else if(this.mode == 3) {
					getMyFansList(pageIndex, pageSize).then(res => {
						this.total = res.data.total
						let filterList = []
						res.data.list.forEach(item => {
							if(item !== null && item !== undefined) {
								item.show_time = cutDateFromDateTime(item.created_at)
								filterList.push(item)
							}
						})
						let pageList = filterList
						if (pageIndex == 0) {
							this.dataList = pageList
						} else {
							this.dataList = this.dataList.concat(pageList)
						}
						let hasNext = res.data.list.length >= pageSize
						if (this.mescroll) {
							this.mescroll.endSuccess(this.dataList.length, hasNext);
						}
					}).catch(error => {
						if (this.mescroll) {
							this.mescroll.endErr()
						}
					})
				}
			},
			downCallback() {
				if (this.mescroll) {
					this.mescroll.resetUpScroll()
				}
			},
			upCallback(page) {
				let pageIndex = page.num - 1
				this.getDataList(pageIndex, page.size)
			}
		}
	}
</script>

<style>
</style>
